package com.iocup.keybastion.sso.common;

import org.apache.commons.codec.binary.Base64;

import java.security.MessageDigest;

/**
 * @author xyjxust
 * @create 2022/3/24 9:21
 **/
public class ChallengeDigestsUtils {


    public static String digest(String verifier, String challengeMethod) {
        try {
            byte[] bytes = verifier.getBytes("UTF-8");
            MessageDigest md = MessageDigest.getInstance(challengeMethod);
            md.update(bytes, 0, bytes.length);
            byte[] digest = md.digest();
            //生成verifier加密后的数据
            return Base64.encodeBase64URLSafeString(digest);
        } catch (Exception e) {
            throw new RuntimeException(String.format("加载摘要算法错误,原因：%s", e.getMessage()));
        }
    }
}
